//
//  VEMainOtherViewController.m
//  VEMoneyPorject
//
//  Created by BlueWind on 2018/9/6.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "VEMainOtherViewController.h"
#import "VEHmoeOtherTableViewCell.h"
#import "VEFastModel.h"

#import "VEHomeViewController.h"
#import "VEHomeTitleDetailTableViewCell.h"
@interface VEMainOtherViewController ()

@end

@implementation VEMainOtherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 可以刷新可以加载更多
    self.refreshType = CJBaseTableVcRefreshTypeRefreshAndLoadMore;
    [self cj_refresh];
}


- (void)cj_refresh{
    [super cj_refresh];
    [self loadData];
}

- (void)loadData{
    [super loadData];
    [self requestDataWithType:1];
    
}

- (void)cj_loadMore{
    [super cj_loadMore];
    NSLog(@"%ld",self.page);
    [self requestDataWithType:2];
    
}

- (void)requestDataWithType:(NSInteger)type{
    
    NSDictionary *dic = [NSDictionary new];
    NSString *url = @"";
    
    if (self.indexType == 1) { // 推荐
        dic = @{@"Page":@(self.page)};
        url = [NSString stringWithFormat:@"%@/VEService/GetTuiJianArticles.do",kPublicInterfaceAPI];
    } else if (self.indexType == 2) { // 行情
        dic = @{@"Page":@(self.page)};
        url = [NSString stringWithFormat:@"%@/VEService/GetHangQingArticles.do",kPublicInterfaceAPI];
    } else if (self.indexType == 3) {
        dic = @{@"Type":@"1", @"Page":@(self.page)};
        url = @"";
    } else if (self.indexType == 4) {
        dic = @{@"Type":@"1", @"Page":@(self.page)};
        url = @"";
    } else if (self.indexType == 5) {
        dic = @{@"Type":@"1", @"Page":@(self.page)};
        url = @"";
    }

    CJBaseRequest *request = [CJBaseRequest cj_requestWithUrl:url isPost:YES withParameters:dic];
    [request cj_sendRequestWithCompletion:^(id response, BOOL success, NSString *message) {
        [self cj_endRefresh];
        [self cj_endLoadMore];
        self.isSuccessNet = YES;
        if (![NSObject isNilOrNull:response]) {
            NSArray *arr = response;
            if (arr.count == 0) {
                self.page -=1;
                return;
            }
            if (type == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [VEFastModel mj_objectArrayWithKeyValuesArray:arr];
            } else {
                NSArray *arr1 = [VEFastModel mj_objectArrayWithKeyValuesArray:arr];
                [self.dataArray addObjectsFromArray:arr1];
            }
        }

        [self.tableView reloadData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)cj_numberOfSections {
    return 1;
}

/**
 某个cell的数量
 */
- (NSInteger)cj_numberOfRowsInsection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)cj_cellHeightAtIndexPath:(NSIndexPath *)indexPath {
     VEFastModel *model = self.dataArray[indexPath.row];
    if ([CJUIKit isNilOrNull:model.ARTICLEMSG]) {
        return UITableViewAutomaticDimension;
    }else{
        return 98;
    }
}

/**
 某行的cell
 */
- (CJBaseTableViewCell *)cj_cellAtIndexPath:(NSIndexPath *)indexPath {
    VEFastModel *model = self.dataArray[indexPath.row];
    if ([CJUIKit isNilOrNull:model.ARTICLEMSG]) {
        VEHomeTitleDetailTableViewCell *cell = [VEHomeTitleDetailTableViewCell nibWithTableView:self.tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        return cell;
    }else{
        VEHmoeOtherTableViewCell *cell =[VEHmoeOtherTableViewCell cellWithTableView:self.tableView];
        cell.model = model;
        return cell;
    }
    
}

- (void)cj_didSelectCellAtIndexPath:(NSIndexPath *)indexPath cell:(CJBaseTableViewCell *)cell {
    VEHomeViewController *vc = [VEHomeViewController new];
    vc.model = self.dataArray[indexPath.row];
    [self pushVc:vc];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
